package net.frankish.moviebank2.front;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.net.URL;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

import net.frankish.moviebank.beans.Movie;
import net.frankish.moviebank.util.Translator;
import net.frankish.tools.toolbar2.FrankishBar;
import net.frankish.tools.toolbar2.SelectAction;

public class MovieBankMenu extends JFrame{
	public enum MovieBankView {VIEW_MOVIES,VIEW_SEARCH,VIEW_STATS,VIEW_SPLASH, VIEW_RANDOM,VIEW_ADD, GENRE_MANAGE};

	private static final URL urlUtilities = MovieBankMenu.class.getResource( "/net/frankish/moviebank/images/64_tools.png" );
	private static final URL urlMagic = MovieBankMenu.class.getResource( "/net/frankish/moviebank/images/32_8ball.png" );

	private static final URL urlMultimedia = MovieBankMenu.class.getResource( "/net/frankish/moviebank/images/64_multimedia.png" );
	private static final URL urlMovies = MovieBankMenu.class.getResource( "/net/frankish/moviebank/images/32_strip.png" );
	private static final URL urlInfo = MovieBankMenu.class.getResource( "/net/frankish/moviebank/images/32_info.png" );
	
	private static final URL urlAdd = MoviePanel.class.getResource( "/net/frankish/moviebank/images/24_add.png" );

	private static final long serialVersionUID = 1L;
	private FrankishBar menuBar = null;
	private JPanel eastPanel = new JPanel();
	private JPanel currentPane = null;
	private static MovieBankMenu instance = null;
	public static MovieBankMenu getInstance(){
		if(instance == null)
			instance = new MovieBankMenu();
		return instance;
	}
	private MovieBankMenu(){
		super();
		this.setSize(800, 600);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setContentPane(getJContentPane());
		URL res = getClass().getResource( "/net/frankish/moviebank/images/32_strip.png" );
		this.setIconImage((new ImageIcon(res)).getImage());
		this.setLocationRelativeTo(null);
		this.setTitle("MovieBank (c) 2010");
		this.setExtendedState(JFrame.MAXIMIZED_BOTH);
		this.addWindowListener(new java.awt.event.WindowAdapter() {
			@Override
			public void windowClosing(java.awt.event.WindowEvent e) {
				windowClosingAction();
			}
		});
		displayView(MovieBankView.VIEW_SPLASH);
	}
	protected void windowClosingAction() {
		setVisible(false);
	}
	
	private Container getJContentPane() {
		eastPanel.setLayout(new BorderLayout());
		eastPanel.setBorder(BorderFactory.createBevelBorder(1));

		menuBar = new FrankishBar();
		JPanel contentPane = new JPanel();
		contentPane.setLayout(new BorderLayout());
		contentPane.add(menuBar,BorderLayout.WEST);
		contentPane.add(eastPanel,BorderLayout.CENTER);
//		contentPane.add(StatusBar.getInstance(),BorderLayout.SOUTH);
		Color options = Color.getHSBColor(0.166666667f, 0.60f, 1f);
		Color movies = Color.getHSBColor(0.62222222f, 0.60f, 1f);
		SelectAction movieView = new SelectAction("Movies",urlMovies,Translator.get("mainmenu.view.movies.tooltip"),MovieBankView.VIEW_MOVIES.toString()){
			private static final long serialVersionUID = 1L;
			@Override
			public void actionPerformed(ActionEvent event) {
				displayView(MovieBankView.VIEW_MOVIES);
			}
		};
		SelectAction infoView = new SelectAction("Info",urlInfo,"Information about MovieBank",MovieBankView.VIEW_STATS.toString()){
			private static final long serialVersionUID = 1L;
			@Override
			public void actionPerformed(ActionEvent event) {
				displayView(MovieBankView.VIEW_STATS);
			}
		};
		SelectAction magicBall = new SelectAction("Magic 8 Ball",urlMagic,"Mmm.. What do you feel like?",MovieBankView.VIEW_RANDOM.toString()){
			private static final long serialVersionUID = 1L;
			@Override
			public void actionPerformed(ActionEvent event) {
				displayView(MovieBankView.VIEW_RANDOM);
			}
		};
		SelectAction addMovie = new SelectAction("Add Movie",urlAdd,"Create a new movie",MovieBankView.VIEW_ADD.toString()){
			private static final long serialVersionUID = 1L;
			@Override
			public void actionPerformed(ActionEvent event) {
				displayView(MovieBankView.VIEW_ADD);
			}
		};
		
		SelectAction manageGenre = new SelectAction("Manage genres",urlAdd,"Add, remove or rename movie genres",MovieBankView.GENRE_MANAGE.toString()){
			private static final long serialVersionUID = 1L;
			@Override
			public void actionPerformed(ActionEvent event) {
				displayView(MovieBankView.GENRE_MANAGE);
			}
		};

		menuBar.addIcon("MoviesOptions","","View the list of your movies, statistic, etc...",urlMultimedia, movies ,movieView);
		menuBar.addIcon("MoviesOptions","",null,null, null ,addMovie);
		menuBar.addIcon("MoviesOptions","",null,null, null ,manageGenre);
		menuBar.addIcon("MoviesOptions","",null,null, null ,infoView);
		menuBar.addIcon("Utilities","","Search, See Favorites and other tools...",urlUtilities, options ,magicBall);
		return contentPane;
	}


	public void displayView(MovieBankView view){
		eastPanel.removeAll();
		if(view == null) view =  MovieBankView.VIEW_SPLASH;
		switch(view){
			case VIEW_SPLASH: currentPane = new JPanel();break;
			case VIEW_MOVIES: currentPane = MovieListingPanel.getInstance();break;
//			case VIEW_STATS: currentPane = SplashPanel.getInstance();break;
//			case VIEW_ADD: MovieEditPanel.showDialog(this, new MovieBean());break;
//			case ABOUT_VIEW: currentPane = new JPanel();break;
//			case EMPTY_VIEW: currentPane = new JPanel();break;
		}
		eastPanel.add(currentPane,BorderLayout.CENTER);
		eastPanel.revalidate();
		eastPanel.repaint();
	}

	@Override
	public void setVisible(boolean b) {
		super.setVisible(b);
		if(b){
			if(getExtendedState() == JFrame.ICONIFIED)setExtendedState(JFrame.NORMAL);
			this.requestFocus();
		}
	}
}
